/*******************************************************************************
*                                 ZLG
*                       ----------------------------
*                       innovating embedded platform
*
* Copyright (c) 2001-present Guangzhou ZHIYUAN Electronics Co., Ltd.
* ALL rights reserved.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
*
* The License of this software follows LGPL v2.1, See the LICENSE for more details:
* https://opensource.org/licenses/LGPL-2.1
*
* Contact information:
* web site:    http://www.zlg.cn/
*******************************************************************************/

#ifndef __RISCV_CORE_H
#define __RISCV_CORE_H

/* \brief 中断使能*/
#define MSTATUS_UIE         (0x1 << 0)    /* 用户/应用程序中断使能 */
#define MSTATUS_SIE         (0x1 << 1)    /* 管理员中断使能 */
#define MSTATUS_MIE         (0x1 << 3)    /* 机器中断使能 */

#define MIE_MEIE            (0x1 << 11)

/* \breif 写控制状态寄存器，内嵌汇编 */
#define write_csr(csr_num, v) __asm volatile("csrw %0, %1" : : "i"(csr_num), "r"(v))

/* \breif 读取控制状态寄存器，内嵌汇编 */
#define read_csr(csr_num) (                                             \
        {                                                               \
            uint32_t v;                                                 \
            __asm volatile("csrr %0, %1" : "=r"(v) : "i"(csr_num)); v;  \
        })

/* \breif 设置控制状态寄存器位，内嵌汇编 */
#define set_csr(csr_num, bit) __asm volatile("csrs %0, %1" : : "i"(csr_num), "r"(bit))

/* \breif 清除控制状态寄存器位，内嵌汇编 */
#define clear_csr(csr_num, bit) __asm volatile("csrc %0, %1" : : "i"(csr_num), "r"(bit))

/* \breif 读取清除控制状态寄存器位，内嵌汇编 */
#define read_clear_csr(csr_num, bit) ({ volatile uint32_t v = 0; __asm volatile("csrrc %0, %1, %2" : "=r"(v) : "i"(csr_num), "r"(bit)); v; })

#define fencei() __asm volatile("fence.i")

#endif  /* __RISCV_CORE_H */
